package com.sigmazero13.bgjeeves.widgets;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sigmazero13.bgjeeves.model.GamePlayerInfo;
import com.sigmazero13.bgjeeves.R;

public class PlayerEntryView extends LinearLayout {
	private Context mContext;
	
	private View detailView;
	private ImageView expander;
	
	private AutoCompleteTextView nameField;
	private AutoCompleteTextView usernameField;
	private MultiAutoComplete teamFields;
	private EditText startingPosField;
	private EditText scoreField;
	private CheckBox winnerBox;
	
	public PlayerEntryView(Context context) {
		super(context);
		commonSetup(context);
	}
	
	public PlayerEntryView(Context context, AttributeSet attrs) {
		super(context, attrs);
		commonSetup(context);
	}
	
	
	private void commonSetup(Context context) {
		mContext = context;
		LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.player_entry, this);
		
		detailView = findViewById(R.id.player_info_details);
		expander = (ImageView)findViewById(R.id.player_expander);
		
		expander.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				toggleExpand();
			}
		});
		
		nameField = (AutoCompleteTextView)findViewById(R.id.player_name);
		usernameField = (AutoCompleteTextView)findViewById(R.id.username);
		teamFields = (MultiAutoComplete)findViewById(R.id.team_list);
		startingPosField = (EditText)findViewById(R.id.starting_pos);
		scoreField = (EditText)findViewById(R.id.score);
		winnerBox = (CheckBox)findViewById(R.id.winner);
	}
	
	public void toggleExpand() {
		int vis = VISIBLE;
		int expandImg = R.drawable.expander_ic_maximized;
		if (detailView.getVisibility() == VISIBLE) {
			vis = GONE;
			expandImg = R.drawable.expander_ic_minimized;
		}
		detailView.setVisibility(vis);
		expander.setImageResource(expandImg);
	}
	
	public void hideScoreAndWinner() {
		findViewById(R.id.player_score_row).setVisibility(GONE);
		findViewById(R.id.player_winner_row).setVisibility(GONE);
	}
	
	public String getPlayerName() {
		return nameField.getText().toString();
	}
	
	public void setPlayerName(String name) {
		nameField.setText(name);
	}
	
	public String getUsername() {
		return usernameField.getText().toString();
	}
	
	public void setUsername(String username) {
		usernameField.setText(username);
	}
	
	public List<String> getTeams() {
		List<String> teams = new ArrayList<String>();
		for (EditText teamField : teamFields.getAllInputs()) {
			teams.add(teamField.getText().toString());
		}
		return teams;
	}
	
	public void setTeams(List<String> teams) {
		teamFields.deleteAllRows();
		if (teams == null) {
			teamFields.addNewInput();
			return;
		}
		for (String team : teams) {
			EditText teamField = teamFields.addNewInput();
			teamField.setText(team);
		}
	}
	
	public Integer getStartingPos() {
		try {
			return Integer.parseInt(startingPosField.getText().toString());
		} catch (NumberFormatException nfe) {
			return null;
		}
	}
	
	public void setStartingPos(Integer startingPos) {
		if (startingPos != null)
			startingPosField.setText(startingPos.toString());
		else
			startingPosField.setText(null);
	}
	
	public Integer getScore() {
		try {
			return Integer.parseInt(scoreField.getText().toString());
		} catch (NumberFormatException nfe) {
			return null;
		}
	}
	
	public void setScore(Integer score) {
		if (score != null) 
			scoreField.setText(score.toString());
		else
			scoreField.setText(null);
	}
	
	public boolean getWinner() {
		return winnerBox.isChecked();
	}
	
	public void setWinner(boolean winner) {
		winnerBox.setChecked(winner);
	}
	
	public GamePlayerInfo getPlayerInfo() {
		GamePlayerInfo player = new GamePlayerInfo();
		player.setName(getPlayerName());
		player.setUsername(getUsername());
		player.setTeams(getTeams());
		player.setStartingPos(getStartingPos());
		player.setScore(getScore());
		player.setWinner(getWinner());
		
		return player;
	}
	
	public void setPlayerInfo(GamePlayerInfo player) {
		Integer tempStart = player.getStartingPos();
		
		if (tempStart == null || tempStart < 0) { 
			tempStart = null;
		}
		
		setPlayerName(player.getName());
		setUsername(player.getUsername());
		setTeams(player.getTeams());
		setStartingPos(tempStart);
		setScore(player.getScore());
		setWinner(player.getWinner());
	}
	
	public void setPlayerList(List<String> playerList) {
		if (playerList == null) {
			playerList = new ArrayList<String>();
		}
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, R.layout.list_item, playerList);
		nameField.setAdapter(adapter); 
	}
	
	public void setUsernameList(List<String> usernameList) {
		if (usernameList == null) {
			usernameList = new ArrayList<String>();
		}
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, R.layout.list_item, usernameList);
		usernameField.setAdapter(adapter);
	}
	
	public void setTeamList(List<String> teamList) {
		teamFields.setTeamList(teamList);
	}
}
